package com.bungarus.busi;

import com.bungarus.codec.protobuf.MessageProtobuf;
import com.bungarus.event.SessionTransferedEvent;
import com.bungarus.model.MessageBuilder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

/**
 * Created by tang on 2018/11/13.
 */
public class AuthResponseHandler extends ChannelInboundHandlerAdapter {
    Logger logger = Logger.getLogger(AuthResponseHandler.class);
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof SessionTransferedEvent){
            logger.debug("+++++++++++++++++++++++++++");
            SessionTransferedEvent event = (SessionTransferedEvent) evt;
            MessageProtobuf.Message message = MessageBuilder.buildAuthSucceed(
                    event.getSession().getId(), event.getSession().getToken());
            ctx.writeAndFlush(message);
        }
        else {
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
